
//author marcel

package ch.sfug.anim.transition
{
	import ch.sfug.events.TweenEvent;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;

	public class Transition extends EventDispatcher
	{
		
		private var _clipHolder:Sprite;
		private var _clipBitmap:Bitmap;
		private var _clip:Sprite;
		private var _time:Number;
		private var _easeFunc:Function;
		private var _type:String;
		private var _played:Number;
		private var _actionAfterTransition:String = null;
		 
		
		public function Transition( clip:Sprite = null, time:Number = 1000, type:String = TransitionType.OUT )
		{
			super();
			
			_clip 		= clip;
			_time 		= time;
			_type		= type;
		}
		
		public function onTransitionEnd( event:TweenEvent ):void
		{
			cleanUp();
			dispatchEvent( event );
		}
		
		public function onTransitionUpdate( event:TweenEvent ):void
		{
			dispatchEvent( event );
		}
		
		public function onTransitionStart():void
		{
			dispatchEvent( new TweenEvent(TweenEvent.TWEEN_START) );
		}
		
		protected function prepareForTransition():void
		{

			_clipHolder 	= new Sprite(); 
			_clip.visible 	= true;
			
			var cParent:DisplayObjectContainer	= _clip.parent;
			var cDepth:int						= cParent.getChildIndex(_clip);
			cParent.removeChild(_clip);
				
			_clipHolder.addChild(_clip);		
			cParent.addChildAt(_clipHolder, cDepth);
		};
		
		
		protected function cleanUp():void
		{
			if (_clipHolder == null || _clip.parent != _clipHolder) return;
			
			var cParent:DisplayObjectContainer  = _clipHolder.parent;
			var cDepth:int 						= cParent.getChildIndex(_clipHolder);
			
			cParent.removeChild(_clipHolder);
			cParent.addChildAt(_clip, cDepth);
			
			_clipHolder = null;
						
			if(_actionAfterTransition == null)return;
			setActionAfterTransition();
		};
		
		protected function setActionAfterTransition():void
		{
			if(actionAfterTransition == "visible")
			{
				_clip.visible = true;
			}
			else if(actionAfterTransition == "hide")
			{
				_clip.visible = false;
			}
			else if(actionAfterTransition == "delete")
			{
				_clip.parent.removeChild(_clip);
			}
		};
		
		public function stopTransition():void{};
				
		public function startTransition():void{};
		
		//getter and setter
		public function set actionAfterTransition( val:String ):void
		{
			_actionAfterTransition = val;
		}
		
		public function get actionAfterTransition():String
		{
			return _actionAfterTransition;
		}
		
		public function get parentClip():Sprite
		{
			return _clip;
		}
		
		public function get clip():Sprite
		{
			return _clipHolder;
		}
		
		public function set time( val:Number ):void
		{
			_time = val;
		}
		
		public function get time():Number
		{
			return _time;
		}

				
		public function set type( val:String ):void
		{
			_type = val;
		}
		
		public function get type():String
		{
			return _type;
		}
		
		public function set played( val:Number ):void
		{
			_played = val;
		}
		
		public function get played():Number
		{
			return _played;
		}
		
		
		
		
	}
}